package edu.ccut.saturn.component.loader.node;

/**
 * 用于解析action组件配置文件中“子action”结点的加载类。
 * <p>继承了<code>NodeInfoLoader</code>,并实现了<code>loadNode</code>方法。<p>
 * 
 * @author 谷钰
 * @author 温建
 * 
 * @version 1.0 2008.8.18
 * 
 * @see <code>SubActionNodeInfo</code>
 */
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.IParameterInfo;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.impl.node.SubActionNodeInfo;

public class SubActionComponentNodeInfoLoader extends NodeInfoLoader {

	public static final String KEY_NODETYPE_ACTION = "action";

	@SuppressWarnings("unchecked")
	public List<INodeInfo> loadNode(Element root, Bundle bundle)
			throws SaturnComponentException {

		List<Element> actionComponentElements = root
				.elements(KEY_NODETYPE_ACTION);

		List<INodeInfo> actionComponentNodeInfos = new ArrayList<INodeInfo>(
				actionComponentElements.size());

		for (Element actionComponentElement : actionComponentElements) {
			String key = actionComponentElement.elementTextTrim(KEY_NODE_KEY);
			try {
				String id = actionComponentElement.attributeValue(KEY_NODE_ID)
						.trim();

				String x = actionComponentElement.attributeValue(KEY_NODE_X)
						.trim();
				String y = actionComponentElement.attributeValue(KEY_NODE_Y)
						.trim();

				String name = actionComponentElement
						.elementTextTrim(KEY_NODE_NAME);
				String description = actionComponentElement
						.elementTextTrim(KEY_NODE_DESCRIPTION);

				String nextId = actionComponentElement.attributeValue(
						KEY_NODE_NEXT).trim();

				logger.debug("  load action node id = \"" + id + "\" key = \"" + key 
						+ "\" name = \"" + name + "\" description = \"" + description
						+ "\"");

				List<IParameterInfo> parameters = NodeInfoLoader
						.getParameterInfos(bundle, actionComponentElement);

				SubActionNodeInfo actionNodeInfo = new SubActionNodeInfo(id,
						name, description, nextId, parameters, key, x, y);

				actionComponentNodeInfos.add(actionNodeInfo);
			} catch (Exception e) {
				// 如果解析xml出现异常，对整个xml文件的解析将没有意思，所以不在继续解析
				throw new SaturnComponentException(
						"load sub action component node failed! the key is ["
								+ key + "]", e);
			}
		}
		return actionComponentNodeInfos;
	}
}
